package com.cyandpos.pad.presentation;

import android.content.Context;
import android.os.Handler;
import androidx.viewpager.widget.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;

import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.utils.CommonUtils;

/**
 * Created by hp on 2018/1/30.
 */

public class DecoratorViewPager extends ViewPager {
    private ViewGroup parent;
    private MyHandler mHandler;
    public DecoratorViewPager(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public DecoratorViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setNestedpParent(ViewGroup parent) {
        this.parent = parent;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (parent != null) {
            parent.requestDisallowInterceptTouchEvent(true);
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        if (parent != null) {
            parent.requestDisallowInterceptTouchEvent(true);
            Log.e("TAG","onInterceptTouchEvent==="+arg0.getY());
        }
        return super.onInterceptTouchEvent(arg0);
    }

    @Override
    public boolean onTouchEvent(MotionEvent arg0) {
        if (parent != null) {
            parent.requestDisallowInterceptTouchEvent(true);
            Log.e("TAG","onTouchEvent==="+arg0.getY());
        }
        switch (arg0.getAction()){
            case MotionEvent.ACTION_DOWN:
               stopPager();
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                startPager();
                break;

        }
        return super.onTouchEvent(arg0);
    }

    class MyHandler extends Handler implements Runnable {

        @Override
        public void run() {

            setCurrentItem(getCurrentItem()+1);
            postDelayed(this, CommonUtils.getInt(ErpCommonData.imageTime)*1000);

        }

        public void startAuto() {
            //防止开启两次task
            removeCallbacks(this);

            postDelayed(this,CommonUtils.getInt(ErpCommonData.imageTime)*1000);
           if(ErpCommonData.imageTime!=null&&!"".equals(ErpCommonData.imageTime)) {
               postDelayed(this, Integer.parseInt(ErpCommonData.imageTime) * 1000);
           }else{
               postDelayed(this, 2 * 1000);
           }
        }

        public void stopAuto() {
            removeCallbacks(this);
        }
    }

    /**
     * 开启ViewPager的自动轮播
     */
    public void startPager() {

        if (mHandler==null) {
            mHandler = new MyHandler();
        }

        mHandler.startAuto();

    }
    /**
     * 关闭ViewPager的自动轮播
     */
    public void stopPager() {
        if(mHandler!=null) {
            mHandler.stopAuto();
        }

    }
}
